<?php

namespace app\index\controller;

use think\Controller;
use app\index\model\Customer;
use think\Db;

class Consignee extends Controller
{
    // 显示收货人列表
    public function consignee()
    {
        if (empty(session('email'))) {
            $this->error('请先登录!', 'login/login');
        }

        $data = Customer::where('email', session('email'))->select();

        //将默认收货人放在最前面
        for ($i = 0; $i < count($data); $i++) {
            // 默认地址
            if (strcmp($data[$i]->cdefault, '1') == 0) {
                $c = $data[$i];
                unset($data[$i]);
                array_unshift($data, $c);
                break;
            }
        }
        // dump((array)$data);

        $this->assign('Customers', $data);
        return $this->fetch();
    }

    //添加收货人
    public function addCustomer()
    {
        $sname = input('get.addName');
        $email = session('email');
        $mobile = input('get.addPhone');
        $addProvince = input('get.addProvince');
        $addCity = input('get.addCity');
        $addAddr = input('get.addAddr');
        // $cdefault = input('get.cdefault') ? input('get.cdefault') : 0;

        $address = $addProvince . $addCity . $addAddr;


        $data = Db::table('tb_customer')->where("email", session("email"))->find();
        if(empty($data)){ //如果没有收货人，此收货人设为默认
            $cdefault = 1;
        }else{
            $cdefault = 0;
        }


        $result = Db::execute(
            "insert into tb_customer(sname,email,mobile,address, cdefault) values('" . $sname . "','" . $email . "','" . $mobile . "','" . $address . "','" . $cdefault . "')
                "
        );
        return $this->redirect('consignee/consignee');
    }

    //编辑收货人
    public function editCustomer()
    {
        $email = session('email');
        if (empty($email)) {
            $this->error('请先登录!', 'login/login');
        }
        $param = input('post.');

        $customer = Customer::get($param['custID']);
        $customer->sname = $param['editName'];
        $customer->mobile = $param['editPhone'];
        $customer->address = $param['address'];
        $customer->save();
        // return $this->redirect('consignee/consignee');
        return "ok";
    }

    //删除收货人
    public function deleteCustomer()
    {
        $custID = input('get.custID');

        $customer = Customer::get($custID);
        if ($customer) {
            $customer->delete();
            return $this->redirect('consignee/consignee');
        }
    }

    //设置默认
    public function setDefault()
    {
        $param = input('get.');
        //$searchstr = "email = '".session('email')."' and cdefault = 1";
        $data = Db::execute("update tb_customer set cdefault=0 where email = '" . session('email') . "' and cdefault = 1");

        $customer1 = Customer::get($param['custID']);
        $customer1->cdefault = 1;
        $customer1->save();
        return $this->redirect('consignee/consignee');
    }
}
